import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type { FailureReportTableItem } from './types.d';

export async function queryFailureReportList(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: FailureReportTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: FailureReportTableItem[];
  }> = await request('/api/failure-report/v1.0.0/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function updateFailureReport(newInfo: { id: string; remark: string }) {
  if (!newInfo || Object.keys(newInfo).length === 0) {
    return { success: false, message: '处理失败，请填入必要信息' };
  }
  const res = await request('/api/failure-report/v1.0.0/handle', {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '处理成功' };
  return { success: false, data: res?.data, message: res?.message || '处理失败' };
}
